/*
 * PolynomialInputFrame.java
 *
 * Created on Nov 21, 2011, 11:27:51 PM
 */
package gui;
/**
 *
 * @author suzann
 */
/*
 * Import Java Classes
 */
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferStrategy;
import java.math.BigDecimal;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;

import util.Activity;
import util.Authentication;
import util.Constants;
import util.PropertiesLoader;
import util.Validator;

import db.DatabaseManager;
import db.entity.Padbatch;
import db.entity.Polynomial;
import db.entity.PolynomialPK;

public class PolynomialInputFrame extends JFrame {
    /** Creates new form PolynomialInputFrame */
    public PolynomialInputFrame() {
        _instance = this;
        initComponents();
        getPadLotID();
        setDefaultValue();
        toggleTextAndButtons(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jtxA9 = new javax.swing.JTextField();
        jlbA9 = new javax.swing.JLabel();
        jtxA8 = new javax.swing.JTextField();
        jlbA8 = new javax.swing.JLabel();
        jcbArm = new javax.swing.JComboBox();
        jlbA0 = new javax.swing.JLabel();
        jlbTitle = new javax.swing.JLabel();
        jlbA2 = new javax.swing.JLabel();
        jtxA1 = new javax.swing.JTextField();
        jlbA1 = new javax.swing.JLabel();
        jtxA0 = new javax.swing.JTextField();
        jlbA5 = new javax.swing.JLabel();
        jtxA4 = new javax.swing.JTextField();
        jlbA3 = new javax.swing.JLabel();
        jtxA2 = new javax.swing.JTextField();
        jlbA4 = new javax.swing.JLabel();
        jtxA3 = new javax.swing.JTextField();
        jlbA6 = new javax.swing.JLabel();
        jtxA6 = new javax.swing.JTextField();
        jlbA7 = new javax.swing.JLabel();
        jtxA7 = new javax.swing.JTextField();
        jtxA5 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jcbPadLotID = new javax.swing.JComboBox();
        jbtEdit = new javax.swing.JButton();
        jbtSave = new javax.swing.JButton();
        jbtCancel = new javax.swing.JButton();
        jlbPolyFormula = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setMinimumSize(new java.awt.Dimension(425, 453));

        jlbA9.setText("a9 = ");

        jlbA8.setText("a8 = ");

        jcbArm.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "L01", "L02", "L03", "L04", "L05", "L06", "L07", "R01", "R02", "R03", "R04", "R05", "R06", "R07" }));
        jcbArm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbArmActionPerformed(evt);
            }
        });

        jlbA0.setText("a0 = ");

        jlbTitle.setFont(new java.awt.Font("Tahoma", 1, 11));
        jlbTitle.setText("Regression Curve Cohesion Values");

        jlbA2.setText("a2 = ");

        jlbA1.setText("a1 = ");

        jlbA5.setText("a5 = ");

        jlbA3.setText("a3 = ");

        jlbA4.setText("a4 = ");

        jlbA6.setText("a6 = ");

        jlbA7.setText("a7 = ");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setText("Polisher");

        jLabel3.setText("Pad Batch ID:");

        jcbPadLotID.setModel(new javax.swing.DefaultComboBoxModel());
        jcbPadLotID.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbPadLotIDActionPerformed(evt);
            }
        });
        jcbPadLotID.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbPadLotIDActionPerformed(evt);
            }
        });

        jbtEdit.setText("Edit");
        jbtEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtEditActionPerformed(evt);
            }
        });

        jbtSave.setText("Save");
        jbtSave.setEnabled(false);
        jbtSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtSaveActionPerformed(evt);
            }
        });

        jbtCancel.setText("Cancel");
        jbtCancel.setEnabled(false);
        jbtCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel3)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jcbPadLotID, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jcbArm, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jlbA0)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jtxA0, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jlbTitle)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jlbA1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jtxA1, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jlbA2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jtxA2, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jlbA3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jtxA3, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jlbA4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jtxA4, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jlbA5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jtxA5, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jlbA6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jtxA6, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jlbA7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jtxA7, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jlbA8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jtxA8, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jlbA9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jtxA9, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jbtEdit)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtSave)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtCancel)))
                .addContainerGap(222, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jcbArm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jcbPadLotID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jlbTitle)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlbA0)
                    .addComponent(jtxA0, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlbA1)
                    .addComponent(jtxA1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlbA2)
                    .addComponent(jtxA2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlbA3)
                    .addComponent(jtxA3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlbA4)
                    .addComponent(jtxA4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlbA5)
                    .addComponent(jtxA5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlbA6)
                    .addComponent(jtxA6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlbA7)
                    .addComponent(jtxA7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlbA8)
                    .addComponent(jtxA8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlbA9)
                    .addComponent(jtxA9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtEdit)
                    .addComponent(jbtSave)
                    .addComponent(jbtCancel))
                .addContainerGap(179, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        jlbPolyFormula.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jlbPolyFormula.setText("Y = a0 + a1 X + a2 X2 + a3 X3 + a4 X4 + a5 X5 + a6 X6 + a7 X7 + a8 X8 + a9 X9");
        jlbPolyFormula.setFocusable(false);
        jlbPolyFormula.setHorizontalTextPosition(javax.swing.SwingConstants.LEADING);
        jlbPolyFormula.setPreferredSize(new java.awt.Dimension(395, 25));
        getContentPane().add(jlbPolyFormula, java.awt.BorderLayout.PAGE_START);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-441)/2, (screenSize.height-638)/2, 441, 638);
    }// </editor-fold>
        
    private void jcbArmActionPerformed(java.awt.event.ActionEvent evt) {                                       
        setDefaultValue();
    }                                      

    private void jbtEditActionPerformed(java.awt.event.ActionEvent evt) {                                        
        editPolynomial();
    }                                       

    private void jcbPadLotIDActionPerformed(java.awt.event.ActionEvent evt) {                                            
        if(jcbPadLotID.getItemCount() > 0)
    		setDefaultValue();
    }                                           

    private void jbtSaveActionPerformed(java.awt.event.ActionEvent evt) {                                        
        savePolynomial();
    }                                       

    private void jbtCancelActionPerformed(java.awt.event.ActionEvent evt) {                                          
        resetPolynomial();
    	toggleTextAndButtons(false);
    }                                         

    private void getPadLotID()
    {
    	jcbPadLotID.removeAllItems();
    	DatabaseManager db = new DatabaseManager();
    	List<Padbatch> pList = db.getPadBatchID();
    	for(Padbatch pItem : pList)
    		jcbPadLotID.addItem(pItem.getPadBatchID());

    }
    private void setDefaultValue()
    {
    	String armId, padLotId;
        if(jcbArm.getSelectedItem() == null)
            return ;
    	if(jcbPadLotID.getSelectedItem() == null)
    		return ;
    	
    	armId = jcbArm.getSelectedItem().toString();
    	padLotId = jcbPadLotID.getSelectedItem().toString();    	
    	DatabaseManager db = new DatabaseManager();
    	db.entity.Polynomial poly = db.listPolyByArmPadlot(armId, padLotId);
    	
    	if(poly == null)
    	{
        	polyValue[0] = "";
        	polyValue[1] = "";
        	polyValue[2] = "";
        	polyValue[3] = "";
        	polyValue[4] = "";
        	polyValue[5] = "";
        	polyValue[6] = "";
        	polyValue[7] = "";
        	polyValue[8] = "";
        	polyValue[9] = "";
        	jtxA0.setText("");
        	jtxA1.setText("");
        	jtxA2.setText("");
        	jtxA3.setText("");
        	jtxA4.setText("");
        	jtxA5.setText("");
        	jtxA6.setText("");
        	jtxA7.setText("");
        	jtxA8.setText("");
        	jtxA9.setText("");
        	
        	return ;
    	}
    	
    	polyValue[0] = poly.getA0()+"";
    	jtxA0.setText(poly.getA0()+"");
    	
    	polyValue[1] = poly.getA1()+"";
    	jtxA1.setText(poly.getA1()+"");
    	
    	polyValue[2] = poly.getA2()+"";
    	jtxA2.setText(poly.getA2()+"");

    	polyValue[3] = poly.getA3()+"";
    	jtxA3.setText(poly.getA3()+"");

    	polyValue[4] = poly.getA4()+"";
    	jtxA4.setText(poly.getA4()+"");

    	polyValue[5] = poly.getA5()+"";
    	jtxA5.setText(poly.getA5()+"");

    	polyValue[6] = poly.getA6()+"";
    	jtxA6.setText(poly.getA6()+"");

    	polyValue[7] = poly.getA7()+"";
    	jtxA7.setText(poly.getA7()+"");

    	polyValue[8] = poly.getA8()+"";
    	jtxA8.setText(poly.getA8()+"");

    	polyValue[9] = poly.getA9()+"";
    	jtxA9.setText(poly.getA9()+"");
    }
    
    private void editPolynomial()
    {
    	String username = null;
        boolean isEdit = false;
        do
        {
            username = Authentication.authenticateUser();
            if(username.equals("Invalid"))
            {
                JOptionPane.showMessageDialog(this, "Your username is invalid.\nPlease enter your username again.", "Authentication Failure", JOptionPane.WARNING_MESSAGE);
                System.out.println("Authentication failure attempted."); //Can be included into activity log later.
                continue;
            }
            else if (username.equals("Exit"))
            {
                System.out.println("Edit pressure values action terminated."); //Can be included into activity log later.
                break;
            }
            else
            {
                if (username != null)
                {
                    isEdit = true;
                    userID = username;
                    //After Authentication, set all the fields to edittable.
                    toggleTextAndButtons(true);
                }
            }
        }while(isEdit == false);
    }
    
    private void savePolynomial()
    {    	
        if(jcbArm.getSelectedItem() == null)
            return ;
    	if(jcbPadLotID.getSelectedItem() == null)
    		return ;
   	
    	//get all the inputs and validate them
        Container c = this.getContentPane();
        Component[] comps = c.getComponents();
        
        ArrayList<Component> compList = new ArrayList <Component>();
        //looping textfields for validation
   /*
        for (Component comp : comps)
        {
            System.out.println(comp.getClass());  
            if (comp instanceof JTextField)
            {
                compList.add(comp);
            }     
        }
 */
    	compList.add(jtxA0);
    	compList.add(jtxA1);
    	compList.add(jtxA2);
    	compList.add(jtxA3);
    	compList.add(jtxA4);
    	compList.add(jtxA5);
    	compList.add(jtxA6);
    	compList.add(jtxA7);
    	compList.add(jtxA8);
    	compList.add(jtxA9);
        //validate the text fields
        boolean isBlank = Validator.validateBlank(compList);
        if(!isBlank)
        {
        	String armId, padLotId;
        	armId = jcbArm.getSelectedItem().toString();
        	padLotId = jcbPadLotID.getSelectedItem().toString();   
        	
        	PolynomialPK pItem_PK = new PolynomialPK();
        	pItem_PK.setArmID(armId);
        	pItem_PK.setPadLotID(padLotId);

        	Polynomial pItem = new Polynomial();
        	pItem.setId(pItem_PK);
        	pItem.setA0(new BigDecimal(jtxA0.getText()));
        	pItem.setA1(new BigDecimal(jtxA1.getText()));
        	pItem.setA2(new BigDecimal(jtxA2.getText()));
        	pItem.setA3(new BigDecimal(jtxA3.getText()));
        	pItem.setA4(new BigDecimal(jtxA4.getText()));
        	pItem.setA5(new BigDecimal(jtxA5.getText()));
        	pItem.setA6(new BigDecimal(jtxA6.getText()));
        	pItem.setA7(new BigDecimal(jtxA7.getText()));
        	pItem.setA8(new BigDecimal(jtxA8.getText()));
        	pItem.setA9(new BigDecimal(jtxA9.getText()));
        	         	
        	
        	DatabaseManager db = new DatabaseManager();
        	if(!db.updatePolynomial(pItem))
        	{
        		JOptionPane.showMessageDialog(null, "Failed to update Polynomial Value.", "Update Failed", JOptionPane.WARNING_MESSAGE);
        		return ;
        	}

        	String activityLog_action = "Update Polynomial Value for Station "+armId+":"+padLotId +": ";
        	activityLog_action += "a0:"+jtxA0.getText()+", ";
        	activityLog_action += "a1:"+jtxA1.getText()+", ";
        	activityLog_action += "a2:"+jtxA2.getText()+", ";
        	activityLog_action += "a3:"+jtxA3.getText()+", ";
        	activityLog_action += "a4:"+jtxA4.getText()+", ";
        	activityLog_action += "a5:"+jtxA5.getText()+", ";
        	activityLog_action += "a6:"+jtxA6.getText()+", ";
        	activityLog_action += "a7:"+jtxA7.getText()+", ";
        	activityLog_action += "a8:"+jtxA8.getText()+", ";
        	activityLog_action += "a9:"+jtxA9.getText();
        	Activity.log(activityLog_action, userID, "");
        	
        	polyValue[0] = jtxA0.getText();
        	polyValue[1] = jtxA1.getText();
        	polyValue[2] = jtxA2.getText();
        	polyValue[3] = jtxA3.getText();
        	polyValue[4] = jtxA4.getText();
        	polyValue[5] = jtxA5.getText();
        	polyValue[6] = jtxA6.getText();
        	polyValue[7] = jtxA7.getText();
        	polyValue[8] = jtxA8.getText();
        	polyValue[9] = jtxA9.getText();
        	
        	toggleTextAndButtons(false);
        	
        	JOptionPane.showMessageDialog(null, "Polynomial Values successfully updated.", "Update Successfully", JOptionPane.PLAIN_MESSAGE);
        }
    }
    
    private void resetPolynomial()
    {
    	jtxA0.setText(polyValue[0].toString());
    	jtxA1.setText(polyValue[1].toString());
    	jtxA2.setText(polyValue[2].toString());
    	jtxA3.setText(polyValue[3].toString());
    	jtxA4.setText(polyValue[4].toString());
    	jtxA5.setText(polyValue[5].toString());
    	jtxA6.setText(polyValue[6].toString());
    }
    
    private void toggleTextAndButtons(boolean isEdittable)
    {
        //If user select 'save', the isEdittable should be false
        //else if user select 'edit', the isEdittable should be true
    	jtxA0.setEditable(isEdittable);
    	jtxA1.setEditable(isEdittable);
    	jtxA2.setEditable(isEdittable);
    	jtxA3.setEditable(isEdittable);
    	jtxA4.setEditable(isEdittable);
    	jtxA5.setEditable(isEdittable);
    	jtxA6.setEditable(isEdittable);
    	jtxA7.setEditable(isEdittable);
    	jtxA8.setEditable(isEdittable);
    	jtxA9.setEditable(isEdittable);
        
        //set the availability of button
        jbtSave.setEnabled(isEdittable);
        jbtCancel.setEnabled(isEdittable);
        jbtEdit.setEnabled(!isEdittable);
        jcbArm.setEnabled(!isEdittable);
        jcbPadLotID.setEnabled(!isEdittable);
    }
    
    public static void updatePadBatchID()
    {
    	_instance.getPadLotID();
    }

    
    public void addNotify() {
        super.addNotify();
        // Buffer
        createBufferStrategy(2);           
        //BufferStrategy strategy = getBufferStrategy();
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PolynomialInputFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PolynomialInputFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PolynomialInputFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PolynomialInputFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new PolynomialInputFrame().setVisible(true);
            }
        });
    }
    
    /****************************************************************************
     * INTERNAL OPERATIONS : THE FRAME
     ****************************************************************************/
    
    /**
     * Prompts up a confirmation dialog to confirm the exit action.
     * Upon confirmation, dispose the frame.
     */
    private void onExit()
    {
            this.dispose();
            resetPolynomial();
            toggleTextAndButtons(false);
    }
    
    /**
     * This is a hack for user to click on the 'x' button on window
     */
    protected void processWindowEvent(WindowEvent we)
    {
            if(we.getID() == WindowEvent.WINDOW_CLOSING)
                    onExit();
    }
    
    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton jbtCancel;
    private javax.swing.JButton jbtEdit;
    private javax.swing.JButton jbtSave;
    private javax.swing.JComboBox jcbArm;
    private javax.swing.JComboBox jcbPadLotID;
    private javax.swing.JLabel jlbA0;
    private javax.swing.JLabel jlbA1;
    private javax.swing.JLabel jlbA2;
    private javax.swing.JLabel jlbA3;
    private javax.swing.JLabel jlbA4;
    private javax.swing.JLabel jlbA5;
    private javax.swing.JLabel jlbA6;
    private javax.swing.JLabel jlbA7;
    private javax.swing.JLabel jlbA8;
    private javax.swing.JLabel jlbA9;
    private javax.swing.JLabel jlbPolyFormula;
    private javax.swing.JLabel jlbTitle;
    private javax.swing.JTextField jtxA0;
    private javax.swing.JTextField jtxA1;
    private javax.swing.JTextField jtxA2;
    private javax.swing.JTextField jtxA3;
    private javax.swing.JTextField jtxA4;
    private javax.swing.JTextField jtxA5;
    private javax.swing.JTextField jtxA6;
    private javax.swing.JTextField jtxA7;
    private javax.swing.JTextField jtxA8;
    private javax.swing.JTextField jtxA9;
    // End of variables declaration
    
    // Programmer declared variable
	private static PolynomialInputFrame _instance ;
    private String userID;
	private String [] polyValue = new String[10];
}
